package org.jrtaudio;

/**
 * RtAudio provides a jni to the RtAudio library.  
 * 
 * @author ben
 */
public class JRtAudio {
	// Stream Status
	static public final int RTAUDIO_INPUT_OVERFLOW = 0x1;    // Input data was discarded because of an overflow condition at the driver.
	static public final int RTAUDIO_OUTPUT_UNDERFLOW = 0x2;  // The output buffer ran low, likely causing a gap in the output sound.

	public JRtAudio()	
	{	
		init();
	}	

	// initialise/deinitialise the system
	public native void init();
	public native void deinit();

	/** 
	 *  Refer to the RtAudio documentation about these functions.
	 */

	public native int getDeviceCount();
	public native DeviceInfo getDeviceInfo(int device);	
	public native int getDefaultOutputDevice();
	public native int getDefaultInputDevice();

	/// opens a stream
	/// differences from RtAudio:
	/// - it returns the actual number of buffer frames used
	/// - callback is an object with an appropriate callback function (depending on the data format used)
	/// the function must be: "public int callback(type[] outputBuffer, type[] inputBuffer, int nBufferFrames, double streamTime, int status)"
	/// where (type) is one of byte, short, int, float, or double
	public native int openStream(StreamParameters out, StreamParameters in, long format, int sampleRate, int bufferFrames, Object callback, StreamOptions options);

	/// create a float32 stream with default stream options
	public int openStream(StreamParameters out, StreamParameters in, int sampleRate, int bufferFrames, Object callback)
	{
		return openStream(out,in,DeviceInfo.RTAUDIO_FLOAT32,sampleRate,bufferFrames,callback,new StreamOptions());
	}
	
	/// open the default Output Stream
	public int openStreamOut(Object callback)
	{
		int did = getDefaultOutputDevice();
		DeviceInfo di = getDeviceInfo(did);
		StreamParameters sp = new StreamParameters();
		sp.deviceId = did;
		sp.nChannels = di.outputChannels;
		return openStream(sp,null,DeviceInfo.RTAUDIO_FLOAT32,44100,512,callback,new StreamOptions());
	}
	
	
	/// create the default output stream
	
	public native void closeStream();
	public native void startStream();
	public native void stopStream();
	public native void abortStream();
	public native boolean isStreamOpen();
	public native boolean isStreamRunning();
	public native double getStreamTime();
	public native long getStreamLatency();
	public native int getStreamSampleRate();
	public native void showWarnings(boolean value);
	public native void showWarnings();	

	/* low level stuff */
	// rtAudioIndex is used by the jni interface to bind an RtAudio and JRtAudio object together
	// don't change this!
	private int rtAudioIndex;

	private static native void initIDs();
	static {
		System.loadLibrary("JRtAudio");
		initIDs();
	}
}
